using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 

namespace TayoukiNameSpace
{
    public partial class DragImages : UserControl
    {

        public int shapeType;

        Bitmap lBitmapdrawingArea;
        Bitmap lBitmapOriginalDrawingArea;
        public System.Windows.Forms.TextBox textInstructions;
        public System.Windows.Forms.PictureBox avatarPictureBox;
     
       
        List<IShape> lListShapes = new List<IShape>();
        List<IShape> lBoxShapes = new List<IShape>();
        List<IShape> lCircleShapes = new List<IShape>();
        List<IShape> lCircleBoxShapes = new List<IShape>();
        List<IShape> lCircleBBoxShapes = new List<IShape>();
        List<IShape> lPieShapes = new List<IShape>();
        List<IShape> lPieBoxShapes = new List<IShape>();

      

     

        /// <summary>
        /// Initialize the control
        /// </summary>
        public DragImages()
        {
           InitializeComponent();
   
           /*
            * Create a blank canvas for drawing
            */
           lBitmapOriginalDrawingArea = new Bitmap(
                this.ClientRectangle.Width,
                this.ClientRectangle.Height,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);

           lBitmapdrawingArea = (Bitmap)lBitmapOriginalDrawingArea.Clone();

           
        }
         


        public void setTextInstuctions(System.Windows.Forms.TextBox textInstructions) {
            this.textInstructions = textInstructions;
           
        }


        public void setAvatarPictureBox(System.Windows.Forms.PictureBox avatarPictureBox)
        {
            this.avatarPictureBox= avatarPictureBox;
        }

        /// <summary>
        /// Creates a new rectangle
        /// </summary>
        public void DrawNewRectangle()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(200, 50);
            Size lSize = new Size(100, 100);
            Brush lBrush = Brushes.Red;
            this.shapeType = 1;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush, shapeType);

            // Add shape to the collection

            lListShapes.Add(lShape);
            
            // Draw image on the bitmap
           
            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();
           
        }

        public void DrawBoxRectangle()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(400,300);
            Size lSize = new Size(100, 100);
            Brush lBrush = Brushes.White;
            this.shapeType = 1;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush,shapeType);
            
            // Add shape to the collection

            lBoxShapes.Add(lShape);
            
           
            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }

        public void DrawBoxCircle()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(500, 500);
            Size lSize = new Size(100, 100);
            Brush lBrush = Brushes.White;
            this.shapeType = 2;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush, shapeType);

            // Add shape to the collection

            lCircleBoxShapes.Add(lShape);


            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }

        public void DrawBBoxCircle()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(300, 200);
            Size lSize = new Size(500, 500);
            Brush lBrush = Brushes.Yellow;
            this.shapeType = 4;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush, shapeType);

            // Add shape to the collection

            lCircleBBoxShapes.Add(lShape);


            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }


        public void DrawNewCircle()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(100, 300);
            Size lSize = new Size(100, 100);
            Brush lBrush = Brushes.Red;
            this.shapeType = 2;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush,shapeType);

            // Add shape to the collection

            lCircleShapes.Add(lShape);

            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }

        public void DrawBoxPie()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(550, 200);
            Size lSize = new Size(200, 200);
            Brush lBrush = Brushes.White;
            this.shapeType = 3;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush, shapeType);

            // Add shape to the collection

            lPieBoxShapes.Add(lShape);


            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }

        public void DrawNewPie()
        {
            System.Drawing.Point lLocation = new System.Drawing.Point(10, 400);
            Size lSize = new Size(200, 200);
            Brush lBrush = Brushes.Red;
            this.shapeType = 3;
            IShape lShape = ShapeFactory.Rectangle(lLocation, lSize, lBrush, shapeType);

            // Add shape to the collection

            lPieShapes.Add(lShape);

            // Draw image on the bitmap

            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);
            lShape.Draw(lGraphics);
            lGraphics.Dispose();

            // Draw bitmap onto the form

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmapdrawingArea, lLocation);
            lGraphicsForm.Dispose();

        }
        
        /// <summary>
        /// On mouse down either switch a dragged shape to a static shape
        /// or change a static shape to a dragged shape.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragImages_MouseDown(object sender, MouseEventArgs e)
        {
            
          
            int lX = e.X ;
            int lY = e.Y ;
            System.Drawing.Point lpntMouseLocation = new System.Drawing.Point(lX, lY);

            lBitmapdrawingArea = (Bitmap)lBitmapOriginalDrawingArea.Clone();
            Graphics lGraphics = Graphics.FromImage(lBitmapdrawingArea);

            //Draw out the non dragged shapes and non selected on the Canvas to be saved;

            foreach (IShape bShape in this.lCircleBBoxShapes)
            {
                bShape.IsDragged = false;
                bShape.Brush = Brushes.Yellow;
                //this.Cursor = this.DefaultCursor;
                bShape.Draw(lGraphics);
            }

            foreach (IShape pShape in this.lListShapes)
            {
                /*
                 * If we are not a dragged shape but our mouse is contained in our
                 * shape then we want to become a dragged shape
                 */
                if (!pShape.IsDragged && pShape.IsCollision(lpntMouseLocation))
                {
                    System.Drawing.Point lpntOffset = new System.Drawing.Point(pShape.Location.X - lpntMouseLocation.X,pShape.Location.Y - lpntMouseLocation.Y);
                    pShape.MouseOffset = lpntOffset;
                    pShape.IsDragged = true;
                    continue;
                }
                else
                {
                    // we are just a static shape that needs to be drawn to the canvas

                    pShape.IsDragged = false;
                    pShape.Brush = Brushes.Red;
                    this.Cursor = this.DefaultCursor;
                    pShape.Draw(lGraphics);

                }

            }
            
            foreach (IShape bShape in this.lBoxShapes)
            {
                foreach(IShape lShape in this.lListShapes){
                        if (Math.Abs(bShape.Location.X - lShape.Location.X) < 10 && Math.Abs(bShape.Location.Y - lShape.Location.Y) < 10)
                        {
                            this.textInstructions.Text = "Good Job!";
                            lShape.IsDragged = false;
                        }
                        else
                        {
                            this.textInstructions.Text = "Move all the shapes inside to the Yellow circle !";
                        }
                }
                bShape.IsDragged = false;
                bShape.Brush = Brushes.White;
                //this.Cursor = this.DefaultCursor;
                
                bShape.Draw(lGraphics);
            }


            foreach (IShape pShape in this.lCircleShapes)
            {
                /*
                 * If we are not a dragged shape but our mouse is contained in our
                 * shape then we want to become a dragged shape
                 */
                if (!pShape.IsDragged && pShape.IsCollision(lpntMouseLocation))
                {
                    System.Drawing.Point lpntOffset = new System.Drawing.Point(pShape.Location.X - lpntMouseLocation.X, pShape.Location.Y - lpntMouseLocation.Y);
                    pShape.MouseOffset = lpntOffset;
                    pShape.IsDragged = true;
                    continue;
                }
                else
                {
                    // we are just a static shape that needs to be drawn to the canvas

                    pShape.IsDragged = false;
                    pShape.Brush = Brushes.Red;
                    this.Cursor = this.DefaultCursor;
                    pShape.Draw(lGraphics);

                }

            }

           



            foreach (IShape bShape in this.lCircleBoxShapes)
            {
                foreach (IShape lShape in this.lCircleShapes)
                {
                    if (Math.Abs(bShape.Location.X - lShape.Location.X) < 10 && Math.Abs(bShape.Location.Y - lShape.Location.Y) < 10)
                    {
                        this.textInstructions.Text = "Good Job!";
                        lShape.IsDragged = false;
                    }
                    else
                    {
                        this.textInstructions.Text = "Move all the shapes inside to the Yellow circle !";
                    }
                }
                bShape.IsDragged = false;
                bShape.Brush = Brushes.White;
                //this.Cursor = this.DefaultCursor;

                bShape.Draw(lGraphics);
            }


            foreach (IShape pShape in this.lPieShapes)
            {
                /*
                 * If we are not a dragged shape but our mouse is contained in our
                 * shape then we want to become a dragged shape
                 */
                if (!pShape.IsDragged && pShape.IsCollision(lpntMouseLocation))
                {
                    System.Drawing.Point lpntOffset = new System.Drawing.Point(pShape.Location.X - lpntMouseLocation.X, pShape.Location.Y - lpntMouseLocation.Y);
                    pShape.MouseOffset = lpntOffset;
                    pShape.IsDragged = true;
                    continue;
                }
                else
                {
                    // we are just a static shape that needs to be drawn to the canvas

                    pShape.IsDragged = false;
                    pShape.Brush = Brushes.Red;
                    this.Cursor = this.DefaultCursor;
                    pShape.Draw(lGraphics);

                }

            }

            foreach (IShape bShape in this.lPieBoxShapes)
            {
                foreach (IShape lShape in this.lPieShapes)
                {
                    if (Math.Abs(bShape.Location.X - lShape.Location.X) < 10 && Math.Abs(bShape.Location.Y - lShape.Location.Y) < 10)
                    {
                        this.textInstructions.Text = "Good Job!";
                        lShape.IsDragged = false;
                    }
                    else
                    {
                        this.textInstructions.Text = "Move all the shapes inside to the Yellow circle !";
                    }
                }
                bShape.IsDragged = false;
                bShape.Brush = Brushes.White;
                //this.Cursor = this.DefaultCursor;

                bShape.Draw(lGraphics);
            }



            lGraphics.Dispose();

            // Draw out the dragged shapes on the Cavas not to save;
            Bitmap lBitMapTemp = (Bitmap)lBitmapdrawingArea.Clone();
            lGraphics = Graphics.FromImage(lBitMapTemp);
            foreach (IShape pShape in this.lListShapes)
            {
                if (pShape.IsDragged)
                {
                    pShape.Location = lpntMouseLocation;
                    pShape.Brush = Brushes.Blue;
                    pShape.Draw(lGraphics);
                }


            }

            foreach (IShape pShape in this.lCircleShapes)
            {
                if (pShape.IsDragged)
                {
                    pShape.Location = lpntMouseLocation;
                    pShape.Brush = Brushes.Blue;
                    pShape.Draw(lGraphics);
                }


            }

             
            foreach (IShape pShape in this.lPieShapes)
            {
                if (pShape.IsDragged)
                {
                    pShape.Location = lpntMouseLocation;
                    pShape.Brush = Brushes.Blue;
                    pShape.Draw(lGraphics);
                }


            }
            lGraphics.Dispose();

            // draw the canvas to the control's surface

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitMapTemp, new System.Drawing.Point(0, 0));
            lGraphicsForm.Dispose();
           
        }
        /// <summary>
        /// Draw our dragged images as the user moves the mouse
        /// Change the mouse cursor when over a static shape if nothing
        /// is being dragged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragImages_MouseMove(object sender, MouseEventArgs e)
        {
            Bitmap lBitmap = (Bitmap)lBitmapdrawingArea.Clone();
            Graphics lGraphics = Graphics.FromImage(lBitmap);
            int lX = e.X ;
            int lY = e.Y;
            System.Drawing.Point lLocation = new System.Drawing.Point(lX, lY);

            Cursor lCursor = null;
            foreach (IShape pShape in this.lListShapes)
            {
                //if the mouse cursor is over a shpae then change it.

                if (pShape.IsCollision(new System.Drawing.Point(lX, lY)))
                {
                    lCursor = pShape.GetCursor();

                }

                // if dragged then change its location 
                if (pShape.IsDragged)
                {

                    pShape.Location = lLocation;
                    pShape.Draw(lGraphics);
                }

            }


            foreach (IShape pShape in this.lCircleShapes)
            {
                //if the mouse cursor is over a shpae then change it.

                if (pShape.IsCollision(new System.Drawing.Point(lX, lY)))
                {
                    lCursor = pShape.GetCursor();

                }

                // if dragged then change its location 
                if (pShape.IsDragged)
                {

                    pShape.Location = lLocation;
                    pShape.Draw(lGraphics);
                }

            }

            foreach (IShape pShape in this.lPieShapes)
            {
                //if the mouse cursor is over a shpae then change it.

                if (pShape.IsCollision(new System.Drawing.Point(lX, lY)))
                {
                    lCursor = pShape.GetCursor();

                }

                // if dragged then change its location 
                if (pShape.IsDragged)
                {

                    pShape.Location = lLocation;
                    pShape.Draw(lGraphics);
                }

            }
            // change the cursor if over a shape
            if (lCursor != null)
            {
                this.Cursor = lCursor;

            }
            else
            {
                this.Cursor = this.DefaultCursor;
            }
            lGraphics.Dispose();

            // draw the bitmap canvas to the control's surface

            Graphics lGraphicsForm = this.CreateGraphics();
            lGraphicsForm.DrawImage(lBitmap, new System.Drawing.Point(0, 0));
            lGraphicsForm.Dispose();
        }
         


    }
}
